<?php
/**
 * Created by PhpStorm.
 * User: admin
 * cookie��count��ʾ����ʾ��news������
 * Date: 2015/8/13
 * Time: 8:16
 */
namespace Home\Controller;

use Think\Controller;
use Think\Exception;
use Think\Log;

class NewsController extends Controller
{
    //默认加载更多条数
    const DEFAULT_LOAD_MORE_NEWS_COUNT = 10;

    /**
     *
     */
    public function news()
    {
        $newsTable = M('mes_publish');
        $tb_column = M('column_info');
        $columnId = I('typeId');//栏目

        $news = $newsTable->where("columnId=%d and status=1",$columnId)->order('publisherTime desc')->limit('10')->select();
        $column = $tb_column->where("Id=" . $columnId)->getField("column");
        if (count($news) == 0) {
            $this->assign('column', $column);
            $this->assign('count', 0);
            $this->display();
            return;
        }
        for ($i = 0; $i < count($news); $i++) {
            $news[$i]['homepage'] = C('REMOTE_IMG_BASE_URL') . $news[$i]['homepage'];
        }
        //dump($news);die;
        $this->assign('news', $news);
        $this->assign('column', $column);
        $this->assign('count', count($news));
        $this->assign('columnId', $columnId);
        cookie("count", count($news));
        $this->display();
    }

    /**
     *news 详情
     */
    public function detail()
    {
        //echo I('newsId');die;
        $newId = I('newsId');
        $newsTable = M("mes_publish");
        $news = $newsTable->where("id='%s'",$newId)->select();
        if (count($news) == 0) {
            $this->redirect('Index/index');
        }
        $news[0]['homepage'] = C('REMOTE_IMG_BASE_URL') . $news[0]['homepage'];
        $tb_column = M('column_info');
        //
        $news[0]['column'] = $tb_column->where("id=%d",$news[0]['columnid'])->getField('column');
        //dump($news);die;
        $this->assign('news', $news);
        $this->display();
    }

    /**
     * 加载更多 ajax
     */
    public function more()
    {
        //$end = I('end');
        $columnId = I('column');
        $newsTable = null;
        try {
            $newsTable = M('mes_publish');
        } catch (Exception $e) {
            return;
        }
        $end = cookie("count");
        if (!$end) {
            $end = 0;
        }
        $news = $newsTable->where("columnId=%d and status=1",$columnId)->order("publisherTime desc")->limit($end . "," . self::DEFAULT_LOAD_MORE_NEWS_COUNT)->select();

        for ($i = 0; $i < count($news); $i++) {
            $news[$i]['homepage'] = C('REMOTE_IMG_BASE_URL') . $news[$i]['homepage'];
        }

        $data["news"] = $news;
        $data["count"] = count($news);
        cookie("count", count($news) + $end);
        if ($data["count"] == 0) {
            $data["status"] = "0";
        } else {
            $data["status"] = "1";
        }
        exit(json_encode($data));
    }

}